package com.xxxx.paipaicars.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxxx.paipaicars.bean.BaseBean;
import com.xxxx.paipaicars.bean.CommodityDO;
import com.xxxx.paipaicars.bean.dto.CommodityDTO;
import com.xxxx.paipaicars.bean.vo.CommodityVO;
import com.xxxx.paipaicars.common.CommodityTypeEnum;
import com.xxxx.paipaicars.common.CommonConstant;
import com.xxxx.paipaicars.dao.CommodityMapper;
import com.xxxx.paipaicars.exception.BusinessException;
import com.xxxx.paipaicars.service.ICommodityService;
import com.xxxx.paipaicars.utils.BeanCopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 北海不是海
 * @version 1.0
 * Create by 2023-06-12 22:07
 * 展示车辆页面
 */
@Slf4j
@Service
public class CommodityServiceImpl extends PageServiceImpl<CommodityMapper, CommodityDO> implements ICommodityService {

    @Resource
    private CommodityMapper commodityMapper;

    /**
     * 通过类型筛选车辆
     * 通过分页的形式进行
     * @param commodityDTO 车辆类型
     * @return
     */
    @Override
    public List<CommodityVO> commodityList(CommodityDTO commodityDTO) {
        //TODO:第一步进行参数的校验
        paramCheckForCommodityList(commodityDTO);
        //TODO:进行对车辆类型的校验
        CommodityTypeEnum commodityTypeEnum = CommodityTypeEnum.valueOf(commodityDTO.getType());
        if(Objects.isNull(commodityTypeEnum)){
            log.warn("输入车辆类型参数有误");
        }
        //TODO:当参数校验完成的时候进行分页的查询
        Page<CommodityDO> CommodityPage = commodityMapper.selectPage(
                super.SetAndGetPage(commodityDTO.getPageNum(), commodityDTO.getPageSize()),
                new LambdaQueryWrapper<CommodityDO>().eq(CommodityDO::getType, commodityTypeEnum.getCode()).
                        eq(BaseBean::getDeleteFlag, CommonConstant.DELETE_FLAG));

        //TODO:对参数的校验
        if(Objects.isNull(CommodityPage) || CollectionUtils.isEmpty(CommodityPage.getOrders())){
            log.warn("无需要的查询结果！");
            return null;
        }


        return BeanCopyUtil.copyPropertiesList(CommodityPage.getRecords(),CommodityVO.class);
    }

    private void paramCheckForCommodityList(CommodityDTO commodityDTO) {

        Assert.notNull(commodityDTO,"参数传入有误");
        if(StringUtils.isBlank(commodityDTO.getType())){
            log.warn("车辆参数类型有误");
            throw new BusinessException("参数错我");
        }

        Assert.notNull(commodityDTO.getPageNum(),"参数不能为null");
        Assert.notNull(commodityDTO.getPageSize(),"参数不能为null");

    }
}
